<?php
class IndexController extends Zend_Controller_Action{
	
	public function init(){
	
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$this->response = $this->getResponse();
		$this->response->setHeader('Content-type', 'application/json;charset=UTF-8', true);
		
		$this->response->setHeader('Access-Control-Allow-Origin', '*');
		
		$this->response->setHeader('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
		$this->response->setHeader('Access-Control-Allow-Headers', 'Content-Type');
		
		//$this->response->setHeader('Access-Control-Allow-Origin',"*",true);
		//header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
		//header('Access-Control-Allow-Origin: *');
		//header('content-type: application/json; charset=utf-8');
		//header("access-control-allow-origin: *");
	}
	// disable layout
	public function indexAction(){
	
		$task = $this->_request->getParam("task","getfulldata");
		switch ($task) {
			case "getfulldata":
				$table 		= $this->_request->getParam("table","grammar");
				$curentCid 	= $this->_request->getParam("cid","0");
				$this->response->setBody($this->$table($curentCid));
			;
			break;
			case "getcurentversionid":
				$mModel 		= new Default_Model_Dataversionid();
				$this->response->setBody($mModel->getDataversionId());
			;
			break;
		}
		
		
	}
	public static function filter(&$subject){
		$subject = str_replace('\\', "\\\\", $subject);
		//$subject = str_replace('"', "|||", $subject);
	}
	public static function filter2($subject){
		return $subject = str_replace('"', "rpl01", $subject);
		//$subject = str_replace('"', "|||", $subject);
	}
	public function dataversionid(){
		$mModel 		= new Default_Model_Dataversionid();
		$data 			= $mModel->listItems(null);
		IndexController::filterArray($data);
		$string = '{"tableName":"grammar","data":' .Zend_Json::encode($data)."}";
		IndexController::filter($string);
		return $string;
	}
	public function grammar($cid){
		$mModel 		= new Default_Model_Grammar();
		$data 			= $mModel->listItems(array('cid'=>$cid));
		IndexController::filterArray($data);
		$string = '{"tableName":"grammar","data":' .Zend_Json::encode($data)."}";
		IndexController::filter($string);
		return $string;
	}
	public function kanji($cid){
		$mModel 		= new Default_Model_Kanji();
		$data 			= $mModel->listItems(array('cid'=>$cid));
		IndexController::filterArray($data);
		$string = '{"tableName":"kanji","data":' .Zend_Json::encode($data)."}";
		IndexController::filter($string);
		return $string;
	}
	public function lesson($cid){
		$mModel 		= new Default_Model_Lesson();
		$data 			= $mModel->listItems(array('cid'=>$cid));
		IndexController::filterArray($data);
		$string = '{"tableName":"lesson","data":' .Zend_Json::encode($data)."}";
		IndexController::filter($string);
		return $string;
	}
	public function listening($cid){
		$mModel 		= new Default_Model_Listening();
		$data 			= $mModel->listItems(array('cid'=>$cid));
		IndexController::filterArray($data);
		$string = '{"tableName":"listening","data":' .Zend_Json::encode($data)."}";
		IndexController::filter($string);
		return $string;
	}
	public static function filterArray(&$array){
		foreach ($array as $key => $value) {
			foreach ($array[$key] as $key2 => $value2){
				$array[$key][$key2] =  IndexController::filter2($value2);
			}
		}
	}
	public function question($cid){
	
		$mModel 		= new Default_Model_Question();
		$data 			= $mModel->listItems(array('cid'=>$cid));
		IndexController::filterArray($data);
		$string = '{"tableName":"question","data":' .Zend_Json::encode($data)."}";
		IndexController::filter($string);
		return $string;
	}
	public function reading($cid){
		$mModel 		= new Default_Model_Reading();
		$data 			= $mModel->listItems(array('cid'=>$cid));
		IndexController::filterArray($data);
		$string = '{"tableName":"reading","data":' .Zend_Json::encode($data)."}";
		IndexController::filter($string);
		return $string;
	}
	public function vocabulary($cid){
		$mModel 		= new Default_Model_Vocabulary();
		$data 			= $mModel->listItems(array('cid'=>$cid));
		IndexController::filterArray($data);
		$string = '{"tableName":"vocabulary","data":' .Zend_Json::encode($data)."}";
		IndexController::filter($string);
		return $string;
	}
}